﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReadWriteCsv;

namespace StockAnomalyDetector
{
    // 这是一个计时器， 每次用一次next 都会使时间向走五分钟
    class Clock
    {
        int hour;
        int min;
        int sec;
        public int toInt32()
        {
            return hour * 10000 + min * 100 + sec;
        }
        public void toNext()
        {
            if (min + 5 == 55)
            {
                hour++;
                min = 0;
            }
            else
                min += 5;
            if (hour == 11 && min == 35)
                hour = 13;
        }
        public void toNext(int time)
        {
            while (time > this.toInt32())
                this.toNext();
        }
        public void turnBack()
        {
            if (min == 0)
            {
                hour--;
                min = 55;
            }
            else
                min -= 5;
            if (hour == 13 && min == 30)
                hour = 11;
        }
        public int turnBackToInt()
        {
            int m = min;
            int h = hour;
            if (min == 0)
            {
                h--;
                m = 55;
            }
            else
                m -= 5;
            if (h == 13 && m == 30)
                h = 11;
            return h * 10000 + m * 100;
        }
        public int toNextToInt()
        {
            int m = min;
            int h = hour;
            if (min + 5 == 55)
            {
                h++;
                m = 0;
            }
            else
                m += 5;
            if (h == 11 && m == 35)
                h = 13;
            return h * 10000 + m * 100;
        }
        public void reset()
        {
            hour = 9;
            min = 35;
            sec = 00;
        }
        public Clock()
        {
            hour = 9;
            min = 35;
            sec = 00;
        }

        public static int turnBack(int time)
        {
            int h = time / 10000;
            int m = (time-h*10000) / 100;
            if (m == 0)
            {
                h--;
                m = 55;
            }
            else
                m -= 5;
            if (h == 13 && m == 30)
                h = 11;
            return h * 10000 + m * 100;
        }
    }

    // scanner 是一个提供一段一段时间扫描来获取信息并分析信息的平台
    class Scanner : Pattern
    {
        private Clock endTime;
        private int sliceRows;

        private Flea flea;
        private Fly fly;
        private Autodetector auto;


        public Scanner(CsvRow r) : base(r)
        {
            flea = new Flea();
            fly = new Fly();
            auto = new Autodetector();

            sliceRows = 0;
            endTime = new Clock();
        }

        public override void Analyze()
        {
            ParseRow();
            if (time < 93000)
                // 跳过竞价阶段
                return;
            if (time < endTime.toInt32())
            {
                // 在时间片内，收集数据
                flea.update(price, quantity);
                fly.update(BorS, quantity);
                auto.update(price);

                sliceRows++;
            }
            else
            // 所读时间超出了时间片，则分析并跳转时钟
            {

                if(sliceRows>10)
                {
                    // 这里我们分析数据
                    // 但是如果读到的数据太少就不分析了
                    if (flea.avLevel != 0)
                    {
                        fly.preAvLevel = flea.avLevel;
                        flea.analyzeSlice(endTime);
                        fly.avLevel = flea.avLevel;
                        fly.analyzeSlice(endTime);                     
                    }
                    else
                    {
                        flea.analyzeSlice(endTime);
                    }
                    auto.analyzeSlice(endTime);
                }

                flea.reset();
                fly.reset();  // there is doubt
                
                endTime.toNext(time);

                //  疑似这里要收集一次数据
                auto.update(price);
                flea.update(price, quantity);
                fly.update(BorS,quantity);
                sliceRows = 1;
            }
        }

        public override void End(string sourceName)
        {
            //  分析完一个文件后，分析是否达到条件，并重设数据？
            flea.analyzeRecord(sourceName);
            fly.analyzeRecord(sourceName);
            auto.analyzeRecord(sourceName);
            
        }
    }
}
